package com.threegroup.web.controller;

import com.threegroup.common.utils.Result;
import com.threegroup.common.utils.UserUtils;
import com.threegroup.web.annotation.Login;
import com.threegroup.web.pojo.AddressBookEntity;
import com.threegroup.web.service.AddressBookService;
import org.springframework.data.repository.query.Param;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 *    功能                       请求方式         请求参数
 * 添加地址                        POST           addressBookDTO
 * 修改默认地址                    PUT            addressBookDTO
 * 删除地址                        DELETE         id
 * 默认地址                        GET            userId
 * 设置默认地址                    PUT            addressBookDTO
 * 根据用户ID查询所有地址          GET            addressBookDTO
 * 地址详细信息                    GET            id
 */
@RestController
@RequestMapping("mp/addressBook")
public class AddressBookController {
    @Resource
    private AddressBookService addressBookService;

    @Resource
    private UserUtils userUtils;
    /**
     * 添加地址
     * POST
     * addressBookDTO
     */
    @Login
    @PostMapping
    public Result add(@RequestBody AddressBookEntity addressBookDTO){
        return addressBookService.add(addressBookDTO);
    }

    /**
     * 修改默认地址
     * PUT
     * addressBookDTO
     */
    @PutMapping
    public Result updateAddr(@RequestBody AddressBookEntity addressBookDTO){
        return addressBookService.update(addressBookDTO);
    }

    /**
     * 删除地址
     * DELETE
     * id
     */
    @DeleteMapping("{id}")
    public Result deleteAdrr(@PathVariable Long id){
        return addressBookService.delete(id);
    }


    /**
     * 默认地址 查询显示
     * GET
     * userId
     */
    @Login
    @GetMapping("default")
    public Result defaultAdrr(Long userId){
        userId=userUtils.getUserId();
        return addressBookService.defaultAdrr(userId);
    }

    /**
     * 设置默认地址  更改
     * PUT
     * addressBookDTO
     */
    @Login
    @PutMapping("default")
    public Result setDefaultAdrr(@RequestBody AddressBookEntity addressBookDTO){
        return addressBookService.setDefaultAdrr(addressBookDTO);
    }

    /**
     * 地址详细信息
     * GET
     * id
     */
    @GetMapping("{id}")
    public Result selectByPrimaryKey(@PathVariable("id") Long id){

        return addressBookService.selectByPrimaryKey(id);
    }

    /**
     * 根据用户ID查询所有地址
     * GET
     * addressBookDTO
     */
    @Login
    @GetMapping("list")
    public Result<List<AddressBookEntity>> addressInfo(){
        Result result=new Result();
        result.setData(addressBookService.queryAddr());
        return result;
    }
}
